using System;
using System.Linq;
using HengTong.Model.Db.SupplyChain.Material;
using HengTong.Model.Db.SupplyChain.Warehouse;
using Microsoft.EntityFrameworkCore;


namespace HengTong.ViewModel.SupplyChain.SysMaterialVms
{
    public partial class SysMaterialBatchVm 
    {
        public override bool DoBatchDelete()
        {
            var ids = Ids.Where(e => Guid.TryParse(e, out _)).Select(Guid.Parse).ToList();
            if (ids.Any())
            {
                DC.Set<SysMaterialSkuAttributes>()
                    .Where(e => ids.Contains(e.Sku.MaterialId.Value))
                    .ExecuteDelete();
                DC.Set<SysWarehouseStock>()
                    .Where(e => ids.Contains(e.MaterialId.Value))
                    .ExecuteDelete();
                DC.Set<SysMaterialSkuAttributes>()
                    .Where(e => ids.Contains(e.Sku.MaterialId.Value))
                    .ExecuteDelete();
                DC.Set<SysMaterialSku>()
                    .Where(e => ids.Contains(e.MaterialId.Value))
                    .ExecuteDelete();
                DC.Set<SysMaterialAttributeValue>()
                    .Where(e => ids.Contains(e.Attribute.MaterialId.Value))
                    .ExecuteDelete();
                DC.Set<SysMaterialAttribute>()
                    .Where(e => ids.Contains(e.MaterialId.Value))
                    .ExecuteDelete();
                DC.Set<SysMaterial>()
                    .Where(e => ids.Contains(e.ID))
                    .ExecuteDelete();
                Ids = ids.Select(e => e.ToString()).ToArray();
            }
            return base.DoBatchDelete();
        }
    }
}
